//
//  ZLLoginViewController.swift
//  新浪微博
//
//  Created by 朱玉龙 on 9/26/16.
//  Copyright © 2016 朱玉龙. All rights reserved.
//

import UIKit
import SVProgressHUD


class ZLOAthorController: UIViewController {


    let webView = UIWebView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        
//        SVProgressHUD.show()
        
        setUpItems()
        
        loadWebView()
        
    }

    
    private  func loadWebView() {
        
        webView.frame = view.bounds
        
        webView.delegate = self
        
        view.addSubview(webView)
        
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        
        let url = URL(string: urlString)
        
        let request = URLRequest(url: url!)
        
        webView.loadRequest(request)
    }
    
    
    
  private  func setUpItems() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", imageName: nil, target: self, action: #selector(close))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", imageName: nil, target: self, action: #selector(autoFill))
        
    }
    
    
    
    @objc private func close() {
        
        dismiss(animated: true) { 
            
            SVProgressHUD.dismiss()
        }
        
    }
   
    @objc private func autoFill() {
    
        let jsString = "document.getElementById('userId').value = '15062461406', document.getElementById('passwd').value = 'zhu498910182' "
        
//          let jsString = "document.getElementById('userId').value = 'leiggee@126.com', document.getElementById('passwd').value = 'onoyomg' "
        webView.stringByEvaluatingJavaScript(from: jsString)
        
        
    }
    
    
    
}
//代理方法
extension ZLOAthorController: UIWebViewDelegate {
    
    internal func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.url?.absoluteString
        print(urlString)
       let flog = "code="
        
        if (urlString?.contains(flog))!  {
            
            let query = request.url?.query ?? ""
            
            let code = (query as NSString).substring(from: flog.characters.count)
            
            ZLOAthorViewModel.shareOAthor.loadData(code: code, finished: { (success) in
                
                if !success {
                    
                    SVProgressHUD.showError(withStatus: errorTitle)
                    
                    return
                }
                
              NotificationCenter.default.post(name: NSNotification.Name.init(kChangeRootController), object: "welcome")
                
               
            })
            
            
            return false
        }
        
        return true
    }
    
    internal func webViewDidFinishLoad(_ webView: UIWebView) {
        
        SVProgressHUD.dismiss()
    }
    
}
